package com.tma.mobilechart;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.tma.mobilechart.utilitys.ColorPickerDialog;
import com.tma.mobilechart.utilitys.ColorPickerDialog.OnColorChangedListener;

public class LineChartActivity extends Activity implements OnClickListener,
		ColorPickerDialog.OnColorChangedListener {

	private EditText etLineChartTitle, etLineChartDescription;
	private RadioGroup rdgLineChartWeight;
	private RadioButton rbLineChartWeightSmall, rbLineChartWeightMedium,
			rbLineChartWeightLarge;
	private RadioButton rbLineChartStyleDash, rbLineChartStyleSolid;
	private CheckBox chbLineChartDisplayValue;
	private CheckBox chbLineChart3d;
	private EditText etLineChartXLabel, etLineChartYLabel;
	private Button btnLineChartView, btnLineChartSave, btnLineChartCancel;
	private TextView tvLineChartXLabel, tvLineChartYLabel;

	private Button btnLineChartTitleColor, btnLineChartContentColor,
			btnLineChartValueColor;

	private LinearLayout linearInputGroup;
	private LayoutInflater lInflater;
	private int lineTitleColor, lineContentColor, lineValueColor;
	private ArrayList<Integer> lineGroupColor;
	private OnColorChangedListener lineColorChanged;

	private int colorState = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_editer_linechart);

		// Set default color
		lineTitleColor = 0xFF51A9FF;
		lineContentColor = 0xFF51A9FF;
		lineValueColor = 0xFF51A9FF;

		lineColorChanged = this;

		etLineChartTitle = (EditText) findViewById(R.id.et_linechart_title);
		etLineChartDescription = (EditText) findViewById(R.id.et_linechart_description);

		rbLineChartWeightSmall = (RadioButton) findViewById(R.id.rb_linechart_weight_small);
		rbLineChartWeightMedium = (RadioButton) findViewById(R.id.rb_linechart_weight_medium);
		rbLineChartWeightLarge = (RadioButton) findViewById(R.id.rb_line_weight_large);

		rbLineChartStyleDash = (RadioButton) findViewById(R.id.rb_linechart_style_dash);
		rbLineChartStyleSolid = (RadioButton) findViewById(R.id.rb_linechart_style_solid);

		chbLineChartDisplayValue = (CheckBox) findViewById(R.id.chb_linechart_displayvalue);
		chbLineChart3d = (CheckBox) findViewById(R.id.chb_linechart_3d);
		tvLineChartXLabel = (TextView) findViewById(R.id.tv_linechart_group_xlabel);
		tvLineChartYLabel = (TextView) findViewById(R.id.tv_linechart_group_ylabel);

		etLineChartXLabel = (EditText) findViewById(R.id.et_linechart_xlabel);

		etLineChartXLabel.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				tvLineChartXLabel.setText(etLineChartXLabel.getText()
						.toString());
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}
		});
		etLineChartYLabel = (EditText) findViewById(R.id.et_linechart_ylabel);

		etLineChartYLabel.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				tvLineChartYLabel.setText(etLineChartYLabel.getText()
						.toString());
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		// Button
		btnLineChartView = (Button) findViewById(R.id.button_linechart_view);
		btnLineChartSave = (Button) findViewById(R.id.button_linechart_save);
		btnLineChartCancel = (Button) findViewById(R.id.button_linechart_cancel);

/*		btnLineChartTitleColor = (Button) findViewById(R.id.button_linechart_color_title);
		btnLineChartContentColor = (Button) findViewById(R.id.button_linechart_color_content);
		btnLineChartValueColor = (Button) findViewById(R.id.button_linechart_color_value);*/

		// Layout Inflater
		linearInputGroup = (LinearLayout) findViewById(R.id.linear_input_group);
		lInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		// Add listener
		btnLineChartView.setOnClickListener(this);
		btnLineChartSave.setOnClickListener(this);
		btnLineChartCancel.setOnClickListener(this);

/*		btnLineChartTitleColor.setOnClickListener(this);
		btnLineChartContentColor.setOnClickListener(this);
		btnLineChartValueColor.setOnClickListener(this);
*/
	}

	@Override
	public void colorChanged(int color) {
		// colorState = 0 -> Title Color
		// colorState = 1 -> Content Color
		// colorState = 2 -> Value Color

		/*switch (colorState) {
		case 0:
			lineTitleColor = color;
			btnLineChartTitleColor.setBackgroundColor(lineTitleColor);
			break;
		case 1:
			lineContentColor = color;
			btnLineChartContentColor.setBackgroundColor(lineContentColor);
			break;
		case 2:
			lineValueColor = color;
			btnLineChartValueColor.setBackgroundColor(lineValueColor);
			break;
		default:
			break;
		}*/

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button_linechart_view:

			// event
			if (rbLineChartWeightSmall.isChecked() == true) {
				Toast.makeText(getBaseContext(),
						rbLineChartWeightSmall.getText(), 200).show();
			}

			else if (rbLineChartWeightMedium.isChecked() == true) {
				Toast.makeText(getBaseContext(),
						rbLineChartWeightMedium.getText(), 200).show();

			} else if (rbLineChartWeightLarge.isChecked() == true) {
				Toast.makeText(getBaseContext(),
						rbLineChartWeightLarge.getText(), 200).show();
			}

			// Line style radio
			if (rbLineChartStyleDash.isChecked() == true) {
				Toast.makeText(getBaseContext(), "Line style dash", 200).show();
			} else {
				Toast.makeText(getBaseContext(), "Line style solid", 200)
						.show();
			}

			// Display value check box
			if (chbLineChartDisplayValue.isChecked()) {
				Toast.makeText(getBaseContext(), "Display value", 200).show();
			} else {
				Toast.makeText(getBaseContext(), "None display value", 200)
						.show();
			}
			// Change mode 2d,3d button
			if (chbLineChart3d.isChecked()) {
				Toast.makeText(getBaseContext(), "3d", 200).show();
			} else {
				Toast.makeText(getBaseContext(), "2d", 200).show();
			}

			// start render
			Intent lineIntent = new Intent(v.getContext(),
					RenderLineActivity.class);
			Bundle dataBundle = new Bundle();
			dataBundle.putBoolean("3DStatus", chbLineChart3d.isChecked());
			lineIntent.putExtras(dataBundle);
			startActivityForResult(lineIntent, 0);
			break;
		case R.id.button_linechart_save:
			Toast.makeText(v.getContext(), "Button Save", 200).show();
			break;
		case R.id.button_linechart_cancel:
			finish();
			break;
/*		case R.id.button_linechart_color_title:
			colorState = 0;
			new ColorPickerDialog(v.getContext(), lineColorChanged,
					lineTitleColor).show();
			break;
		case R.id.button_linechart_color_content:
			colorState = 1;
			new ColorPickerDialog(v.getContext(), lineColorChanged,
					lineContentColor).show();
			break;
		case R.id.button_linechart_color_value:
			colorState = 2;
			new ColorPickerDialog(v.getContext(), lineColorChanged,
					lineValueColor).show();
			break;*/

		default:
			break;
		}
	}

	public void addItem(View v) {
		LinearLayout parent = (LinearLayout) ((ViewGroup) v.getParent()
				.getParent()).getChildAt(1);
		View myView = lInflater.inflate(R.layout.dynamic_item_linechart, null);
		parent.addView(myView);
	}

	public void deleteItem(View v) {
		LinearLayout parent = (LinearLayout) v.getParent().getParent();
		parent.removeView((View) v.getParent());
	}

	public void addGroup(View v) {
		View myView = lInflater.inflate(R.layout.dynamic_group_linechart, null);
		linearInputGroup.addView(myView);
	}

	public void deleteGroup(View v) {
		linearInputGroup.removeView((View) v.getParent().getParent());
	}

	public void setLineColor(final View v) {
		int initialColor = Color.parseColor(v.getTag().toString()
				.replace("0x", "#"));
		new ColorPickerDialog(v.getContext(), new OnColorChangedListener() {

			@Override
			public void colorChanged(int color) {
				// TODO Auto-generated method stub
				v.setBackgroundColor(color);
				String tag = "0x" + Integer.toHexString(color);
				v.setTag(tag);
			}
		}, initialColor).show();
	}

}
